/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AdicionarMedicacao.java
 *
 * Created on 01/12/2011, 12:49:20
 */

package view;

import java.awt.Color;
import java.util.ArrayList;
import model.Medicacao;
import util.Observador;

/**
 *
 * @author tais
 */
public class AdicionarMedicacao extends javax.swing.JFrame {
    private Medicacao medicacao;

    private ArrayList<Observador> observers = new ArrayList<Observador>();

    /** Creates new form AdicionarMedicacao */
    public AdicionarMedicacao(Medicacao m) {
        initComponents();
        medicacao = m;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        descricaoLabel = new javax.swing.JLabel();
        descricao = new javax.swing.JTextField();
        posologialabel = new javax.swing.JLabel();
        duracaoLabel = new javax.swing.JLabel();
        duracao = new javax.swing.JTextField();
        posologia = new javax.swing.JTextField();
        salvar = new javax.swing.JButton();
        avisos = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Medicao a ser usada pelo paciente");

        descricaoLabel.setText("Nome(ou descricao): ");

        posologialabel.setText("Posologia:");

        duracaoLabel.setText("Duracao:");

        salvar.setText("Salvar");
        salvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                salvarActionPerformed(evt);
            }
        });

        avisos.setText(" ");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(avisos, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(12, 12, 12)
                                .addComponent(descricao, javax.swing.GroupLayout.DEFAULT_SIZE, 364, Short.MAX_VALUE))
                            .addComponent(descricaoLabel)))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(posologialabel))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addComponent(posologia, javax.swing.GroupLayout.DEFAULT_SIZE, 364, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(duracaoLabel))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addComponent(duracao, javax.swing.GroupLayout.DEFAULT_SIZE, 364, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(162, 162, 162)
                        .addComponent(salvar, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(descricaoLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(descricao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(posologialabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(posologia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(duracaoLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(duracao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(avisos)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 11, Short.MAX_VALUE)
                .addComponent(salvar)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void salvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_salvarActionPerformed
        if(descricao.getText().isEmpty()){
            avisos.setText("Preencha o nome ou descricao da medicação");
            descricaoLabel.setForeground(Color.red);
        }
        else{
            descricaoLabel.setForeground(Color.black);
            if(posologia.getText().isEmpty()){
                avisos.setText("Indique a posologia (ex.: A cada 12 horas)");
                posologialabel.setForeground(Color.red);
            }
            else{
                posologialabel.setForeground(Color.black);
                if(duracao.getText().isEmpty()){
                    avisos.setText("Descreva a duração do tratamento (ex.: Três dias)");
                    duracaoLabel.setForeground(Color.red);
                }
                else{
                    duracaoLabel.setForeground(Color.black);
                    medicacao.setDescricao(descricao.getText());
                    medicacao.setPosologia(posologia.getText());
                    medicacao.setDuracao(duracao.getText());
                    notificar();
                    this.dispose();
                }
            }
        }
    }//GEN-LAST:event_salvarActionPerformed

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel avisos;
    private javax.swing.JTextField descricao;
    private javax.swing.JLabel descricaoLabel;
    private javax.swing.JTextField duracao;
    private javax.swing.JLabel duracaoLabel;
    private javax.swing.JTextField posologia;
    private javax.swing.JLabel posologialabel;
    private javax.swing.JButton salvar;
    // End of variables declaration//GEN-END:variables

     public void notificar(){
        int i;
        for(i=0; i<observers.size(); i++){
            observers.get(i).atualizar(this);
        }
    }

    public void addObservador(Observador o){
        observers.add(o);
    }

    public void setMedicacao(Medicacao medicacao) {
        this.medicacao = medicacao;
    }

    public Medicacao getMedicacao() {
        return this.medicacao;
    }
}
